<?php
if (! defined ( 'BASEPATH' ))
    exit ( 'No direct script access allowed' );

class registrasi_model extends Model {
    
    protected $_table = 'RM';
    protected $_prefix = 'rm';
    
    public function __construct() {
        // Call the Model constructor
        parent::__construct ();
    }

    public function add($data) {
        if (! $data || ! is_array ( $data ))
            return false;
        return $this->db->insert ( $this->_table, $data ) && ($this->db->affected_rows () > 0) ? true : false;
    
    }
    public function update($id, $data) {
        if (! $id || ! $this->get ( $id ) || ! $data)
            return array ();
        $this->db->where ( $this->_prefix . 'Id', $id );
        $this->db->limit ( 1 );
        return $this->db->update ( $this->_table, $data ) && ($this->db->affected_rows () > 0) ? true : false;
    }
	public function delete($id) {
        if (! $id || ! $this->get ( $id ))
            return array ();
        $this->db->where ( $this->_prefix . 'Id', $id );
        $this->db->limit ( 1 );
        return $this->db->delete ( $this->_table ) && ($this->db->affected_rows () > 0) ? true : false;
    } 
    public function get($id='', $where=array()) {
        if ( !$id ) 
            return array ();
        
        $this->db->select ( 'RM.*' );
        $this->db->select ( 'Pasien.*' );    
        $this->db->select ( 'RMDetail.dokterId, RMDetail.userId, RMDetail.rmKeluhan, RMDetail.rmDiagnosa, RMDetail.rmKeterangan, RMDetail.rmTotalBiaya' );
        $this->db->select ( 'Dokter.dokterNama' );    
        
        $this->db->from ( 'RM' );
        $this->db->join ( 'Pasien', 'RM.pasienId = Pasien.pasienId' );
        $this->db->join ( 'RMDetail', 'RM.rmId = RMDetail.rmId', 'left' );
        $this->db->join ( 'Dokter', 'RMDetail.dokterId = Dokter.dokterId', 'left' );
        
        if( isset($id) )
        	$this->db->where ( 'RM.rmId', $id );
        if( isset($where['RM.rmTglMulai']) )
        	$this->db->where ( 'DATE(rmTglMulai)', $where['rmTglMulai'] );
        if( isset($where['RM.rmStatus']) )
        	$this->db->where ( 'rmStatus', $where['rmStatus'] );
        	
        $query = $this->db->get ();	
        return $query->row_array ();
    }
    
    public function getList($where=array(), $limit = 100, $offset = 0) {
    	$this->db->select ( 'RM.*' );
    	$this->db->select ( 'Pasien.*' );
    	$this->db->select ( 'RMDetail.dokterId, RMDetail.userId, RMDetail.rmKeluhan, RMDetail.rmDiagnosa, RMDetail.rmKeterangan, RMDetail.rmTotalBiaya' );
    	$this->db->select ( 'Dokter.dokterNama' );    
    	
    	$this->db->from ( 'RM' );
        $this->db->join ( 'Pasien', 'RM.pasienId = Pasien.pasienId' );
        $this->db->join ( 'RMDetail', 'RM.rmId = RMDetail.rmId', 'left' );
        $this->db->join ( 'Dokter', 'RMDetail.dokterId = Dokter.dokterId', 'left' );

        if( isset($where['rmTglMulai']) )
        	$this->db->where ( 'DATE(rmTglMulai)', $where['rmTglMulai'] );
        if( isset($where['rmStatus']) )
        	$this->db->where_in ( 'rmStatus', $where['rmStatus'] );
        if( isset($where['pasienId']) )
        	$this->db->where ( 'RM.pasienId', $where['pasienId'] );	
        	
        if( !empty($where['frdate']) && empty($where['todate']) )
        	$this->db->where ( 'DATE(rmTglMulai)', $where['frdate'] );	
        elseif( !empty($where['frdate']) && !empty($where['todate']) )
        	$this->db->where ( 'DATE(rmTglMulai) BETWEEN "'.$where['frdate'].'" AND "'.$where['todate'].'" ' );
        
        $this->db->order_by ( 'rmTglMulai' , 'desc');
        $this->db->order_by ( 'rmNoUrut' , 'desc');	
        $this->db->limit ( $limit, $offset );
        $query = $this->db->get ();
        return $query->result_array ();
    }
    public function getCount($where=array()) {
        $this->db->select ( 'count(' . $this->_prefix . 'Id) as count' );
        $this->db->from ( 'RM' );
        $this->db->join ( 'Pasien', 'RM.pasienId = Pasien.pasienId' );

        if( isset($where['rmTglMulai']) )
        	$this->db->where ( 'DATE(rmTglMulai)', $where['rmTglMulai'] );
        if( isset($where['rmStatus']) )
        	$this->db->where_in ( 'rmStatus', $where['rmStatus'] );
		if( isset($where['pasienId']) )
        	$this->db->where ( 'RM.pasienId', $where['pasienId'] );	
        	
        if( !empty($where['frdate']) && empty($where['todate']) )
        	$this->db->where ( 'DATE(rmTglMulai)', $where['frdate'] );	
        elseif( !empty($where['frdate']) && !empty($where['todate']) )
        	$this->db->where ( 'DATE(rmTglMulai) BETWEEN "'.$where['frdate'].'" AND "'.$where['todate'].'" ' );	
        	
        $query = $this->db->get ();
        if ($query && ($row = $query->row_array ())) {
            return $row ['count'];
        }
        return 0;
    }
    
	public function getNoUrut($where=array()) {
        if ( !$where ) 
            return 1;
        
        //$this->db->select ( 'rmId, MAX(rmNoUrut) AS nourut' );
        $this->db->select ( 'rmId, rmNoUrut AS nourut' );    
        $this->db->from ( 'RM' );
        
        if( isset($where['rmTglMulai']) )
        	$this->db->where ( 'DATE(rmTglMulai)', $where['rmTglMulai'] );
        if( isset($where['rmStatus']) )
        	$this->db->where ( 'rmStatus', $where['rmStatus'] );
        	
        $this->db->order_by ( 'rmNoUrut', 'desc' );	
        $this->db->limit (1);
        $query = $this->db->get ();	
        if ($query && ($row = $query->row_array ())) {
            return $row ['nourut'] + 1;
        }
        return 1;
    }
    
	public function getPasienKode() {
        $this->db->select ( 'pasienKode AS kode' );    
        $this->db->from ( 'Pasien' );
        $this->db->order_by ( 'pasienKode', 'desc' );	
        $this->db->limit (1);
        $query = $this->db->get ();	
        if ($query && ($row = $query->row_array ())) {
            return $row ['kode'];
        }
        return 0;
    }
    
	public function getRMKode() {
        $this->db->select ( 'rmKode AS kode' );    
        $this->db->from ( 'RM' );
        
        if( isset($where['rmTglMulai']) )
        	$this->db->where ( 'DATE(rmTglMulai)', $where['rmTglMulai'] );
        if( isset($where['rmStatus']) )
        	$this->db->where ( 'rmStatus', $where['rmStatus'] );
        	
        $this->db->order_by ( 'rmKode', 'desc' );	
        $this->db->limit (1);
        $query = $this->db->get ();	
        if ($query && ($row = $query->row_array ())) {
            return $row ['kode'];
        }
        return 0;
    }
    
    //RM Detail
	public function addRM($data) {
        if (! $data || ! is_array ( $data ))
            return false;
            
        return $this->db->insert ( 'RMDetail', $data ) && ($this->db->affected_rows () > 0) ? true : false;
    
    } 
    
    public function updateRM($id, $data) {
        if ( !$id || !$data )
            return array ();
            
        $this->db->where ( 'rmId', $id );
        $this->db->limit ( 1 );
        return $this->db->update ( 'RMDetail', $data ) && ($this->db->affected_rows () > 0) ? true : false;
    }
    
	//Update Jml RJ Pasien
	public function updateJmlRJ($id) {
        if ( !$id )
            return false;

        return $this->db->query ( "UPDATE Pasien SET pasienJmlRJ = pasienJmlRJ + 1
        							WHERE pasienId = '".$id."' " ) && ($this->db->affected_rows () > 0) ? true : false;
    }
}